package com.smartcommunity.service.sixEntity.impl;
import com.smartcommunity.bean.constant.Constant;
import com.smartcommunity.bean.entity.Camera;
import com.smartcommunity.bean.entity.Village;
import com.smartcommunity.dao.access.AccessDeviceDao;
import com.smartcommunity.dao.camera.CameraDao;
import com.smartcommunity.dao.people.PeopleDao;
import com.smartcommunity.dao.sixEntity.SixEntityDao;
import com.smartcommunity.dao.vehicle.VehicleDao;
import com.smartcommunity.dao.village.RealCompanyDao;
import com.smartcommunity.dao.village.RealPowerOrgDao;
import com.smartcommunity.dao.village.VillageDao;
import com.smartcommunity.entity.people.People;
import com.smartcommunity.entity.village.SixEntity;
import com.smartcommunity.entity.village.SixStatisticsEntity;
import com.smartcommunity.service.sixEntity.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: StatisticsServiceImpl
 * @Description: 统计相关服务层
 */
@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private PeopleDao peopleDao;
    @Autowired
    private VillageDao villageDao;
    @Autowired
    private RealCompanyDao realCompanyDao;
    @Autowired
    private RealPowerOrgDao realPowerOrgDao;
    @Autowired
    private CameraDao cameraDao;
    @Autowired
    private AccessDeviceDao accessDeviceDao;
    @Autowired
    private VehicleDao vehicleDao;
    @Autowired
    private SixEntityDao sixEntityDao;


    /**
     * 一标六实中的六实数量
     * @return
     */
    @Override
    public SixEntity sixRealCount(String villageCode) {
        log.debug(villageCode+"统计一标六实中的六实数量");
        SixEntity sixEntity = new SixEntity();
        Map<String,Integer> resultMap = new HashMap<>();
        Map<String,Object> map = new HashMap<>();
        map.put("villageCode", villageCode);

        // 实有人口
        People people = new People();
        people.setVillageCode(villageCode);
        Map<String,Long>  peopleMap = peopleDao.peopleTypeCount(map);
        // 人口总数
        sixEntity.setPeopleCount(peopleMap.get("peopleCount").intValue());
        // 户籍人员
        sixEntity.setHjPeopleCount(peopleMap.get("hjPeopleCount").intValue());
        // 外来人员
        sixEntity.setWlPeopleCount(peopleMap.get("wlPeopleCount").intValue());
        // 境外人员
        sixEntity.setJwPeopleCount(peopleMap.get("jwPeopleCount").intValue());
        // 临时人员
        sixEntity.setLsPeopleCount(peopleMap.get("lsPeopleCount").intValue());
        // 已登记车辆
        sixEntity.setVehicleCount(vehicleDao.findVehicleListCount(map));
        // 楼栋，房屋数量
        List<Village> villageList = villageDao.listVillagesByVillageCode(map);
        int buildingCount = 0;
        int houseCount =0;
        if(!CollectionUtils.isEmpty(villageList)) {
            for(Village village : villageList) {
                houseCount += Integer.valueOf(isNull(village.getHouseNum(),"0"));
                buildingCount += Integer.valueOf(isNull(village.getBuildingNum(),"0"));
            }
        }
        sixEntity.setBuildingCount(buildingCount);
        sixEntity.setHouseCount(houseCount);

        // 实有单位
        sixEntity.setCompanyCount(realCompanyDao.countRealCompanies(map));

        //所有相机数量
        int cameraCount = 0;
        //普通相机
        int normalCameraCount = 0;
        //人脸相机
        int faceCameraCount = 0;
        //车辆相机
        int vehicleCameraCount = 0;
        List<Camera> cameraList = cameraDao.getCamera(map);
        if(!CollectionUtils.isEmpty(cameraList)) {
            for (Camera camera :cameraList){
                int count = camera.getCount();
                int type = camera.getCameraType();
                // 1：摄像机、2：人脸卡口（人脸抓拍机）、3：车辆卡口（车辆抓拍机）
                if (type == 1) {
                    normalCameraCount += count;
                }else  if (type == 2) {
                    faceCameraCount += count;
                }else  if (type == 3) {
                    vehicleCameraCount += count;
                }
                cameraCount  += count;
            }
        }
        sixEntity.setCameraCount(cameraCount);
        sixEntity.setNormalCameraCount(normalCameraCount);
        sixEntity.setFaceCameraCount(faceCameraCount);
        sixEntity.setVehicleCameraCount(vehicleCameraCount);

        // 实有安防设施
        int securityEquipment = 0;
        // 门禁
        Integer accessCount = accessDeviceDao.getAccessDeviceCount(map);
        securityEquipment += Integer.valueOf(accessCount);
        //实有设备（门禁+摄像机）
        securityEquipment += cameraCount;
        sixEntity.setSecurityEquipmentCount(securityEquipment);

        // 实有实有力量与装备
        Integer realPeopleCount = realPowerOrgDao.countRealPowerByVillage(map);
        sixEntity.setPowerCount(realPeopleCount);
        sixEntity.setEquipmentCount(0);
        return sixEntity;
    }

    /**
     * 批量统计数据
     * @return
     */
    public void statisticsSixEntity() {
        //统计人员数据
        buildPeopleStatisticsSixEntity();

        //统计车辆数据
        buildVehicleStatisticsSixEntity();

        //统计楼栋数据
        buildBuildStatisticsSixEntity();

        //统计房屋数据
        buildHouseStatisticsSixEntity();

        //统计单位数据
        buildCompaniesStatisticsSixEntity();

        //统计摄像机数据
        buildCameraStatisticsSixEntity();

        //统计门禁设备数据
        buildaccessDeviceStatisticsSixEntity();

        //统计实有力量数据
        buildRealPowerStatisticsSixEntity();
    }

    //统计实有力量数据
    private void buildRealPowerStatisticsSixEntity() {
        long st = System.currentTimeMillis();
        try{
            //实有力量
            List<SixStatisticsEntity>  realPowerList =realPowerOrgDao.realPowerCountGroupByVillage();
            for(SixStatisticsEntity realPower : realPowerList){
                realPower.setType(Constant.REALPOWER);
            }
            sixEntityDao.deleteSixEntityCount(Constant.REALPOWER);
            if(CollectionUtils.isNotEmpty(realPowerList)) {
                sixEntityDao.insertSixEntityCount(realPowerList);
            }
        }catch (Exception ex){
          log.error("实有力量统计数据失败"+ex);
        }
        long speedTime=System.currentTimeMillis()-st;
        System.out.println("实有力量统计数据耗时："+speedTime);

    }

    //统计门禁设备数据
    private void buildaccessDeviceStatisticsSixEntity() {
        long st = System.currentTimeMillis();
        try{
            //门禁设备数量
            List<SixStatisticsEntity>  accessDeviceList = accessDeviceDao.accessDeviceCountGroupByVillage();
            for(SixStatisticsEntity access : accessDeviceList){
                access.setType(Constant.DEVICESTYPE);
            }
            if(CollectionUtils.isNotEmpty(accessDeviceList)) {
                sixEntityDao.insertSixEntityCount(accessDeviceList);
            }
        }catch (Exception ex){
            log.error("门禁设备统计数据失败"+ex);
        }
        long speedTime=System.currentTimeMillis()-st;
        System.out.println("门禁设备统计数据耗时："+speedTime);
    }

    //统计相机设备数据
    private void buildCameraStatisticsSixEntity() {
        long st = System.currentTimeMillis();
        try{
            //摄像机数量
            List<SixStatisticsEntity>  cameraList = cameraDao.cameraCountGroupByVillage();
            for(SixStatisticsEntity camera : cameraList){
                camera.setType(Constant.DEVICESTYPE);
            }
            sixEntityDao.deleteSixEntityCount(Constant.DEVICESTYPE);
            if(CollectionUtils.isNotEmpty(cameraList)) {
                sixEntityDao.insertSixEntityCount(cameraList);
            }
        }catch (Exception ex){
            log.error("相机设备统计数据失败"+ex);
        }
        long speedTime=System.currentTimeMillis()-st;
        System.out.println("相机设备统计数据耗时："+speedTime);
    }
   //实有单位统计数据
    private void buildCompaniesStatisticsSixEntity() {
        long st = System.currentTimeMillis();
        try{
            //实有单位数量
            List<SixStatisticsEntity>  companiesList = realCompanyDao.companiesCountGroupByVillage();
            for(SixStatisticsEntity company : companiesList){
                company.setType(Constant.COMPANYTYPE);
            }
            sixEntityDao.deleteSixEntityCount(Constant.COMPANYTYPE);
            if(CollectionUtils.isNotEmpty(companiesList)) {
                sixEntityDao.insertSixEntityCount(companiesList);
            }
        }catch (Exception ex){
            log.error("实有单位统计数据失败"+ex);
        }
        long speedTime=System.currentTimeMillis()-st;
        System.out.println("实有单位统计数据耗时："+speedTime);
    }
    //房屋统计数据
    private void buildHouseStatisticsSixEntity() {
        long st = System.currentTimeMillis();
        try{
            //房屋数量
            List<SixStatisticsEntity>  houseList = villageDao.houseCountGroupByVillage();
            for(SixStatisticsEntity house : houseList){
                house.setType(Constant.HOUSETYPE);
            }
            sixEntityDao.deleteSixEntityCount(Constant.HOUSETYPE);
            if(CollectionUtils.isNotEmpty(houseList)) {
                sixEntityDao.insertSixEntityCount(houseList);
            }
        }catch (Exception ex){
            log.error("房屋统计数据失败"+ex);
        }
        long speedTime=System.currentTimeMillis()-st;
        System.out.println("房屋统计数据耗时："+speedTime);
    }

    //楼栋统计数据
    private void buildBuildStatisticsSixEntity() {
        long st = System.currentTimeMillis();
        try{
            //楼栋数量
            List<SixStatisticsEntity>  buildList = villageDao.buildCountGroupByVillage();
            for(SixStatisticsEntity build : buildList){
                build.setType(Constant.BUILDTYPE);
            }
            sixEntityDao.deleteSixEntityCount(Constant.BUILDTYPE);
            if(CollectionUtils.isNotEmpty(buildList)) {
                sixEntityDao.insertSixEntityCount(buildList);
            }
        }catch (Exception ex){
            log.error("楼栋统计数据失败"+ex);
        }
        long speedTime=System.currentTimeMillis()-st;
        System.out.println("楼栋统计数据耗时："+speedTime);
    }

    //车辆统计数据
    private void buildVehicleStatisticsSixEntity() {
        long st = System.currentTimeMillis();
        try{
            //车辆数量
            List<SixStatisticsEntity>  vehicleList = vehicleDao.vehicleCountGroupByVillage();
            for(SixStatisticsEntity vehicle : vehicleList){
                vehicle.setType(Constant.VEHICLETYPE);
            }
            sixEntityDao.deleteSixEntityCount(Constant.VEHICLETYPE);
            if(CollectionUtils.isNotEmpty(vehicleList)) {
                sixEntityDao.insertSixEntityCount(vehicleList);
            }
        }catch (Exception ex){
            log.error("车辆统计数据失败"+ex);
        }
        long speedTime=System.currentTimeMillis()-st;
        System.out.println("车辆统计数据耗时："+speedTime);
    }

    //人员统计数据
    private void buildPeopleStatisticsSixEntity() {
        long st = System.currentTimeMillis();
        try {
            //人口数量
            List<SixStatisticsEntity> peopleList = peopleDao.peopleTypeCountGroupByVillage();
            for(SixStatisticsEntity people : peopleList){
                people.setType(Constant.PEOPLETYPE);
            }
            sixEntityDao.deleteSixEntityCount(Constant.PEOPLETYPE);
            if(CollectionUtils.isNotEmpty(peopleList)){
                sixEntityDao.insertSixEntityCount(peopleList);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("人员统计数据失败"+ex);
        }
        long speedTime=System.currentTimeMillis()-st;
        System.out.println("人员统计数据耗时："+speedTime);
    }


    private  String isNull(Object object,String defaultValue) {
        return  object == null || "".equals(object) ? defaultValue : object.toString();
    }

}
